package com.cxj.link;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;

import com.cxj.AppDialog;
import com.waps.AppConnect;

public class StartActivity extends Activity implements View.OnClickListener {
    private Button audioBtn;
    private Button exitBtn;
    private MediaPlayer mMediaPlayer;
    private Button musicBtn;
    private Button practiceBtn;
    private SharedPreferences sp;
    private Button taskBtn;

    private void exit() {
        if ((this.mMediaPlayer != null) && (this.mMediaPlayer.isPlaying()))
            this.mMediaPlayer.stop();
        SoundsManager.destroy();
        onDestroy();
        finish();
    }

    private void initBtn() {
        this.practiceBtn = ((Button) findViewById(R.id.practice_btn));
        this.taskBtn = ((Button) findViewById(R.id.task_btn));
        this.musicBtn = ((Button) findViewById(R.id.music_btn));
        this.audioBtn = ((Button) findViewById(R.id.audio_btn));
        this.exitBtn = ((Button) findViewById(R.id.exit_btn));
        this.practiceBtn.setOnClickListener(this);
        this.taskBtn.setOnClickListener(this);
        this.musicBtn.setOnClickListener(this);
        this.audioBtn.setOnClickListener(this);
        this.exitBtn.setOnClickListener(this);
        initBtnText();
    }

    private void initBtnText() {
        if (!Setting.music)
            this.musicBtn.setText("音乐开");
        else
            this.musicBtn.setText("音乐关");
        if (!Setting.audio)
            this.audioBtn.setText("音效开");
        else
            this.audioBtn.setText("音效关");
    }

    private void initSounds() {
        playSounds();
    }

    private void playSounds() {
        if (!Setting.music) {
            if ((this.mMediaPlayer != null) && (this.mMediaPlayer.isPlaying()))
                this.mMediaPlayer.stop();
        } else {
            this.mMediaPlayer = MediaPlayer.create(this, 2130968576);
            if (!this.mMediaPlayer.isPlaying()) {
                this.mMediaPlayer.setLooping(true);
                this.mMediaPlayer.start();
            }
        }
    }

    private void writeSP() {
        SharedPreferences.Editor localEditor = this.sp.edit();
        localEditor.putBoolean("music", Setting.music);
        localEditor.putBoolean("audio", Setting.audio);
        localEditor.putInt("level1", Setting.level1);
        localEditor.putInt("level2", Setting.level2);
        localEditor.putInt("point", Setting.point);
        localEditor.commit();
    }

    @Override
    public void onClick(View paramView) {
        switch (paramView.getId()) {
        case R.id.practice_btn:
            startActivity(new Intent(this, Linker.class));
            break;
        case R.id.task_btn:
            startActivity(new Intent(this, TaskActivity.class));
            break;
        case R.id.exit_btn:
            exit();
            break;
        case R.id.music_btn:
            Setting.music = !Setting.music;
            playSounds();
            writeSP();
            initBtnText();
            break;
        case R.id.audio_btn:
            Setting.audio = !Setting.audio;
            writeSP();
            initBtnText();
            break;
        default:
            break;
        }
    }

    @Override
    public void onCreate(Bundle paramBundle) {
        super.onCreate(paramBundle);
        AppConnect.getInstance(this);
        AppDialog.show(this);
        requestWindowFeature(1);
        setContentView(R.layout.start);
        this.sp = getSharedPreferences("setting", 0);
        if (this.sp != null) {
            Setting.music = this.sp.getBoolean("music", true);
            Setting.audio = this.sp.getBoolean("audio", true);
            Setting.level1 = this.sp.getInt("level1", 1);
            Setting.level2 = this.sp.getInt("level2", 0);
            Setting.point = this.sp.getInt("point", 0);
        }
        SoundsManager.initSoundPoolMap(this);
        initBtn();
        initSounds();
    }

    @Override
    protected void onDestroy() {
        AppConnect.getInstance(this).finalize();
        super.onDestroy();
    }

    @Override
    public boolean onKeyDown(int paramInt, KeyEvent paramKeyEvent) {
        boolean bool;
        if (paramInt != 4) {
            bool = super.onKeyDown(paramInt, paramKeyEvent);
        } else {
            writeSP();
            exit();
            bool = true;
        }
        return bool;
    }
}
